﻿#region licence
//  Copyright 2008 Michael Cromwell

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.DSL;
using System.Configuration;
using NUnit.Framework.SyntaxHelpers;
using log4net.Core;
using log4net.Appender;
using System.Collections;

namespace log4net.altconf.Tests
{

    public class Log4NetDslConfigurationSyntax
    {
        protected DslFactory dslFactory;

        [SetUp]
        public void Setup()
        {
            dslFactory = new DslFactory();
            dslFactory.Register<TestLog4NetDslConfiguration>(new TestLog4NetDslEngine());
            dslFactory.BaseDirectory = ConfigurationManager.AppSettings["script-base-directory"];
        }

        [TearDown]
        public void Teardown()
        {
            dslFactory = null;
        }
    }

    [TestFixture]
    public class When_dsl_is_supplied_global_options : Log4NetDslConfigurationSyntax
    {
        [Test]
        public void Given_debug_is_set_in_dsl_should_be_set_in_config()
        {
            var config = dslFactory.Create<TestLog4NetDslConfiguration>("global-options-set.boo");
            config.Process();
            Assert.That(config.debug, Is.True);
        }
        
        [Test]
        public void Given_threshold_has_been_set_in_dsl_should_be_set_in_config()
        {
            var config = dslFactory.Create<TestLog4NetDslConfiguration>("global-options-set.boo");
            config.Process();
            Assert.That(config.threshold, Is.EqualTo(Level.Fatal));
        }
                
    }


    [TestFixture]
    public class When_dsl_is_supplied_appender : Log4NetDslConfigurationSyntax
    {

        [Test]
        public void Should_call_add_appender_with_right_name()
        {
            var config = dslFactory.Create<TestLog4NetDslConfiguration>("single-appender.boo");
            config.ListenForAddAppender((name, type) => Assert.That(name, Is.EqualTo("testAppender")));
            config.Process();
        }

        [Test]
        public void Should_call_add_appender_with_right_type()
        {
            var config = dslFactory.Create<TestLog4NetDslConfiguration>("single-appender.boo");
            config.ListenForAddAppender((name, type) => Assert.That(type, Is.EqualTo(typeof(ConsoleAppender))));
            config.Process();
        }
    }


    [TestFixture]
    public class When_dsl_syntax_is_supplied_logs : Log4NetDslConfigurationSyntax
    {
        [Test]
        public void Should_call_log_for_with_correct_name()
        {
            var config = dslFactory.Create<TestLog4NetDslConfiguration>("appender-and-root-log.boo");
            config.ListenForLogFor((name, action) => Assert.That(name, Is.EqualTo("root")));
            config.Process();
        }

        [Test]
        public void Should_call_use_appenders_with_parameters()
        {
            var config = dslFactory.Create<TestLog4NetDslConfiguration>("appender-and-root-log.boo");
            config.ListenForUseAppenders(appenders => Assert.That(appenders.Count(), Is.EqualTo(1)));
            config.ListenForUseAppenders(appenders => Assert.That(appenders[0], Is.EqualTo("testAppender")));
            config.Process();
        }


        [Test]
        public void Given_level_supplied_should_call_at_level_with_correct_parameter()
        {
            var config = dslFactory.Create<TestLog4NetDslConfiguration>("appender-and-root-log.boo");
            config.ListenForAtLevel(lvl => Assert.That(lvl, Is.EqualTo(Level.Fatal)));
            config.Process();
        }
    }
}
